package com.leoman.report.service.impl;

import com.leoman.common.controller.common.CommonController;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.message.dao.MessageDao;
import com.leoman.message.entity.Message;
import com.leoman.post.dao.PostBaseDao;
import com.leoman.post.entity.PostBase;
import com.leoman.report.dao.ReportDao;
import com.leoman.report.entity.Report;
import com.leoman.report.service.ReportService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/7/1 0001.
 *
 */
@Service
public class ReportServiceImpl extends GenericManagerImpl<Report, ReportDao> implements ReportService {

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private PostBaseDao postBaseDao;

    @Autowired
    private MessageDao messageDao;

    @Override
    public Page<Report> page(final String nickname1, final String mobile, final String nickname2, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        Page<Report> page = reportDao.findAll(new Specification<Report>() {
            @Override
            public Predicate toPredicate(Root<Report> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (StringUtils.isNotEmpty(nickname1)) {
                    Predicate pre = cb.like(root.get("userInfo").get("nickname").as(String.class), "%" + nickname1 + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotEmpty(mobile)) {
                    Predicate pre = cb.like(root.get("userInfo").get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotEmpty(nickname2)) {
                    Predicate pre = cb.like(root.get("userPost").get("userInfo").get("nickname").as(String.class), "%" + nickname2 + "%");
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);

        return page;
    }

    @Override
    public List<Report> findListNew() {
        return reportDao.findListNew(CommonController.getOldDate());
    }

    @Override
    @Transactional
    public void deletePost(Long reportId) {
        Report report = reportDao.findOne(reportId);
        report.getPostBase().setDeleteStatus(Constant.POST_STATUS_001);
        postBaseDao.save(report.getPostBase());

        // 新增删帖消息
        Message message = new Message();
        message.setUserId(report.getPostBase().getUserInfo().getId());
        message.setPostId(report.getPostBase().getId());
        message.setType(1);
        message.setToObject("0");
        message.setTitle("帖子被删除");
        message.setContent("管理员删除");
        messageDao.save(message);
        delete(report);
    }

    @Override
    @Transactional
    public void lockPost(Long reportId) {
        Report report = reportDao.findOne(reportId);

        PostBase postBase = report.getPostBase();
        if (postBase != null) {
            postBase.setDeleteStatus(Constant.POST_STATUS_002);
            postBaseDao.save(postBase);
        }

        // 新增锁帖消息
        Message message = new Message();
        message.setUserId(report.getPostBase().getUserInfo().getId());
        message.setPostId(report.getPostBase().getId());
        message.setType(1);
        message.setToObject("0");
        message.setTitle("帖子被锁帖");
        message.setContent("管理员锁帖");
        messageDao.save(message);

        // 修改举报状态
        report.setStatus(Constant.REPORT_STATUS_002);
        reportDao.save(report);
    }
}
